//
//  FPSliderView.swift
//  Flsun3DPrint
//
//  Created by yfb on 2024/5/20.
//

import UIKit
import JXGradientKit


class FCSlider: UISlider {
    var sliderHeight: CGFloat = 12.0
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    func setupUI() {
        maximumTrackTintColor = .clear
        minimumTrackTintColor = .clear
        
        addSubview(titleLabel)
        addSubview(arrowImgView)
        
        insertSubview(lineSelectView, at: 0)
        insertSubview(lineBgView, at: 0)
        
        titleLabel.snp.makeConstraints { make in
            make.size.equalTo(CGSize(width: 50, height: 24))
            make.centerX.equalTo(arrowImgView)
            make.bottom.equalTo(arrowImgView.snp.top)
        }
        
        lineBgView.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(sliderHeight)
            make.centerY.equalToSuperview()
        }
    }
    
    @available(*, unavailable)
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
 
    override func thumbRect(forBounds bounds: CGRect, trackRect rect: CGRect, value: Float) -> CGRect {
        let result = super.thumbRect(forBounds: bounds, trackRect: rect, value: value)
        
        let orign = result.origin
        let size = result.size
        let centerX = orign.x + size.width/2.0
        let centerY = orign.y + size.height/2.0
        arrowImgView.frame = CGRect(origin: CGPoint(x: centerX - 3, y: orign.y - 8), size: CGSize(width: 6, height: 6))
        // 2是 微调距离； 0.5 是微调 y 
        lineSelectView.frame = CGRect(x: 2, y: centerY - sliderHeight/2.0-0.5, width: centerX, height: sliderHeight)
        
        titleLabel.text = "\(Int(value))"
        return result
    }
    
    lazy var titleLabel: GradientLabel = {
        let tLable = GradientLabel()
        
        tLable.textColor = .white
        tLable.font = .systemFont(ofSize: 12)
        tLable.layer.masksToBounds = true
        tLable.layer.cornerRadius = 3
        
        tLable.startColor = .orange
        tLable.endColor = .yellow
        tLable.direction = .leftToRight
        tLable.textAlignment = .center
        return tLable
    }()
    
    lazy var arrowImgView: UIImageView = {
        let tImgV = UIImageView()
        tImgV.isUserInteractionEnabled = true
        tImgV.image = UIImage(named: "")
        return tImgV
    }()
    
    lazy var lineSelectView: GradientView = {
        let tView = GradientView()
        tView.startColor = .orange
        tView.endColor = .yellow
        tView.direction = .leftToRight
        
        tView.layer.masksToBounds = true
        tView.layer.cornerRadius = CGFloat(sliderHeight)/2.0
        
        return tView
    }()
    
    lazy var lineBgView: UIView = {
        let tView = UIView()
        tView.backgroundColor = .gray
        
        tView.layer.masksToBounds = true
        tView.layer.cornerRadius = sliderHeight/2.0
        return tView
    }()
}
